package com.nuonuo.router;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author duan
 * @version 1.0
 * @date 2023/4/7 9:43
 */
public class Main {


    public static void main(String[] args) {
        // 存在以下几种location配置
        TrieRouteMatcher trieRouteMatcher = new TrieRouteMatcher();
        trieRouteMatcher.addRoute("/jz/accounting");
        trieRouteMatcher.addRoute("/jz/service/account/print");
        trieRouteMatcher.addRoute("/jz/accounting/print");
        trieRouteMatcher.addRoute("/jz/accounting/manage/enterprise/");
        trieRouteMatcher.addRoute("/jz/accounting/report/wan");


        // 针对以下几种请求
        String request1 = "http://127.0.0.1:8080/jz/accounting/sayHello?e=1&f=2&g=3";
        String request2 = "http://127.0.0.1:8080/jz/service/account/print/sayHello?e=1&f=2&g=3";
        String request3 = "http://127.0.0.1:8080/jz/accounting/print/sayHello?e=1&f=2&g=3";
        String request4 = "http://127.0.0.1:8080/jz/accounting/manage/enterprise/sayHello?e=1&f=2&g=3";
        String request5 = "http://127.0.0.1:8080/jz/accounting/report/wan/sayHello?e=1&f=2&g=3";

        String path1 = null, path2 = null, path3 = null, path4 = null, path5 = null;
        try {
            URI uri1 = new URI(request1);
            URI uri2 = new URI(request2);
            URI uri3 = new URI(request3);
            URI uri4 = new URI(request4);
            URI uri5 = new URI(request5);
            path1 = uri1.getPath();
            path2 = uri2.getPath();
            path3 = uri3.getPath();
            path4 = uri4.getPath();
            path5 = uri5.getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        String tmpPath1 = path1;
        String tmpPath2 = path2;
        String tmpPath3 = path3;
        String tmpPath4 = path4;
        String tmpPath5 = path5;


        RouteInfo info1 = trieRouteMatcher.match(tmpPath1);
        RouteInfo info2 = trieRouteMatcher.match(tmpPath2);
        RouteInfo info3 = trieRouteMatcher.match(tmpPath3);
        RouteInfo info4 = trieRouteMatcher.match(tmpPath4);
        RouteInfo info5 = trieRouteMatcher.match(tmpPath5);
        // 匹配到的location配置结果
        System.out.println("path1: " + path1 + ", 命中结果：" + (info1 == null? "" : info1.getRoute()));
        System.out.println("path2: " + path2 + ", 命中结果：" + (info2 == null? "" : info2.getRoute()));
        System.out.println("path3: " + path3 + ", 命中结果：" + (info3 == null? "" : info3.getRoute()));
        System.out.println("path4: " + path4 + ", 命中结果：" + (info4 == null? "" : info4.getRoute()));
        System.out.println("path5: " + path5 + ", 命中结果：" + (info5 == null? "" : info5.getRoute()));
    }
}
